﻿<%@ WebHandler Language="C#" Class="GZSkipStepReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 工装工具厂--批次未完工序查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2023.6.12
 **/

public class GZSkipStepReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"
SELECT cci.productname,cci.productdescription,cci.containername,cci.qty,cci.fromworkcentername,od.priority,od.risklevel,od.startdate,od.enddate,
       ws.workflowstepname,sd.durationperunit,
       csd.dispatchdate,csd.dispatchemployeename,csd.reportdate,csd.reportemployeename
FROM containercurrentinfo cci
LEFT JOIN product p ON p.productid = cci.productid
INNER JOIN gzordertypemanagement od ON od.containername = cci.containername
LEFT JOIN factory f ON f.factoryid = cci.originalfactoryid
LEFT JOIN workflowstep ws ON ws.workflowid = NVL(cci.bsworkflowid,cci.workflowid)
LEFT JOIN containersteprecord csd ON csd.containerid = cci.containerid AND csd.workflowstepid = ws.workflowstepid
LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
LEFT JOIN spec s ON s.specid = NVL(sb.revofrcdid,ws.specid)
LEFT JOIN containercommidetail cm ON cm.containerid = cci.containerid AND cm.workflowstepid = ws.workflowstepid AND cm.isuse = 0
LEFT JOIN stepschedulingdetail sd ON sd.stepid = ws.workflowstepid
WHERE s.opdesc <> '虚拟序' AND p.istopgz = 1 AND cci.isinstore = 1 AND cm.containercommidetailid IS NULL AND csd.checkdate IS NULL";
        //分厂
        if (jObject["factoryname"] != null && jObject["factoryname"].ToString() != "")
        {
            sql += string.Format(" AND f.factoryname = '{0}'", jObject["factoryname"].ToString());
        }

        //结束时间
        if (jObject["selectdate"] != null && jObject["selectdate"].ToString() != "")
        {
            JArray selectdate = JArray.Parse(jObject["selectdate"].ToString());
            sql += string.Format(" AND od.enddate >= TO_DATE('{0}','yyyy-mm-dd')", selectdate[0].ToString());
            sql += string.Format(" AND od.enddate <= TO_DATE('{0}','yyyy-mm-dd')", selectdate[1].ToString());
        }

        //主制工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //重要程度
        if (jObject["priority"] != null && jObject["priority"].ToString() != "")
        {
            sql += string.Format(" AND od.priority LIKE '%{0}%'", jObject["priority"].ToString());
        }

        //风险等级
        if (jObject["risklevel"] != null && jObject["risklevel"].ToString() != "")
        {
            sql += string.Format(" AND od.risklevel LIKE '%{0}%'", jObject["risklevel"].ToString());
        }

        //派工人
        if (jObject["dispatcher"] != null && jObject["dispatcher"].ToString() != "")
        {
            sql += string.Format(" AND csd.dispatchemployeename = '{0}'", jObject["dispatcher"].ToString());
        }

        //派工人
        if (jObject["reporter"] != null && jObject["reporter"].ToString() != "")
        {
            sql += string.Format(" AND csd.reportemployeename = '{0}'", jObject["reporter"].ToString());
        }

        sql += @"
UNION ALL
SELECT cci2.productname,cci2.productdescription,cci2.containername,cci2.qty,cci2.fromworkcentername,od.priority,od.risklevel,od.startdate,od.enddate,
       ws.workflowstepname,sd.durationperunit,
       csd.dispatchdate,csd.dispatchemployeename,csd.reportdate,csd.reportemployeename
FROM containercurrentinfo cci
LEFT JOIN product p ON p.productid = cci.productid
INNER JOIN gzordertypemanagement od ON od.containername = cci.containername
LEFT JOIN bomproductsubproduct bom ON bom.productname = cci.productname AND bom.topproductrev = cci.productrevision
LEFT JOIN containercurrentinfo cci2 ON cci2.containerno = cci.containerno AND cci2.productname = bom.subproductname
LEFT JOIN factory f ON f.factoryid = cci2.originalfactoryid
LEFT JOIN workflowstep ws ON ws.workflowid = NVL(cci2.bsworkflowid,cci2.workflowid)
LEFT JOIN containersteprecord csd ON csd.containerid = cci2.containerid AND csd.workflowstepid = ws.workflowstepid
LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
LEFT JOIN spec s ON s.specid = NVL(sb.revofrcdid,ws.specid)
LEFT JOIN containercommidetail cm ON cm.containerid = cci2.containerid AND cm.workflowstepid = ws.workflowstepid AND cm.isuse = 0
LEFT JOIN stepschedulingdetail sd ON sd.stepid = ws.workflowstepid
WHERE s.opdesc <> '虚拟序' AND p.istopgz = 1 AND cci.isinstore = 1 AND cm.containercommidetailid IS NULL AND csd.checkdate IS NULL";
        //分厂
        if (jObject["factoryname"] != null && jObject["factoryname"].ToString() != "")
        {
            sql += string.Format(" AND f.factoryname = '{0}'", jObject["factoryname"].ToString());
        }
        
        //结束时间
        if (jObject["selectdate"] != null && jObject["selectdate"].ToString() != "")
        {
            JArray selectdate = JArray.Parse(jObject["selectdate"].ToString());
            sql += string.Format(" AND od.enddate >= TO_DATE('{0}','yyyy-mm-dd')", selectdate[0].ToString());
            sql += string.Format(" AND od.enddate <= TO_DATE('{0}','yyyy-mm-dd')", selectdate[1].ToString());
        }

        //主制工区
        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND cci2.fromworkcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //零件图号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci2.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci2.containername = '{0}'", jObject["containername"].ToString());
        }

        //重要程度
        if (jObject["priority"] != null && jObject["priority"].ToString() != "")
        {
            sql += string.Format(" AND od.priority LIKE '%{0}%'", jObject["priority"].ToString());
        }

        //风险等级
        if (jObject["risklevel"] != null && jObject["risklevel"].ToString() != "")
        {
            sql += string.Format(" AND od.risklevel LIKE '%{0}%'", jObject["risklevel"].ToString());
        }

        //派工人
        if (jObject["dispatcher"] != null && jObject["dispatcher"].ToString() != "")
        {
            sql += string.Format(" AND csd.dispatchemployeename = '{0}'", jObject["dispatcher"].ToString());
        }

        //派工人
        if (jObject["reporter"] != null && jObject["reporter"].ToString() != "")
        {
            sql += string.Format(" AND csd.reportemployeename = '{0}'", jObject["reporter"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        dt.Columns["productname"].Caption = "零件号";
        dt.Columns["productdescription"].Caption = "零件名称";
        dt.Columns["containername"].Caption = "批次号";
        dt.Columns["qty"].Caption = "数量";
        dt.Columns["fromworkcentername"].Caption = "主制工区";
        dt.Columns["priority"].Caption = "重要程度";
        dt.Columns["risklevel"].Caption = "风险等级";
        dt.Columns["startdate"].Caption = "开始时间";
        dt.Columns["enddate"].Caption = "结束时间";
        dt.Columns["workflowstepname"].Caption = "未完工序";
        dt.Columns["durationperunit"].Caption = "单位工时";
        dt.Columns["dispatchdate"].Caption = "派工时间";
        dt.Columns["dispatchemployeename"].Caption = "派工人";
        dt.Columns["reportdate"].Caption = "报工时间";
        dt.Columns["reportemployeename"].Caption = "报工人";
        ExportHelper.ExportExcel(dt, "批次未完工序查询报表");
    }
}